package com.skytech.component.monitor.enterprise_data_monitoring;


import com.skytech.component.monitor.data.PM25Data;
import com.skytech.component.monitor.data.PM25DataDAO;
import com.skytech.component.monitor.util.ElementAbstractUrl;
import com.skytech.component.monitor.util.FormateConnection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.UUID;

@Service
public class PM25Service  implements  CrawlerScript{


    private static final Logger log = LoggerFactory.getLogger(PM25Service.class);

    @Autowired
    private PM25DataDAO pm25DataDAO;
    @Override
    public void start() {
        String url = "http://www.pm25.in/";
        try{
            Document document =  FormateConnection.createConnection(Jsoup.connect(url))
                    .get();
            Elements allElements = document.getAllElements();
            List<String> urlList=ElementAbstractUrl.links(allElements.select("body > div.container > div.span12.cities > div.all > div.bottom > ul> div:nth-child(2) > li > a"));
            detailData(urlList);
        }catch (Exception e){
            log.error(e.getMessage());
        }finally {
            log.info("数据爬取结束");
        }


    }

    private void detailData(List<String> urlList) {

        for (String url: urlList) {
            try{
                Document document =  FormateConnection.createConnection(Jsoup.connect(url))
                        .get();
                log.info("当前页面--{}",url);
                Elements allElements = document.getAllElements();
                PM25Data pm25Data=new PM25Data();
                pm25Data.setArea(allElements.select("body > div.container > div.span12.avg > div:nth-child(1) > div.city_name > h2").text());
                pm25Data.setCreateTime(allElements.select("body > div.container > div.span12.avg > div:nth-child(2) > div.live_data_time > p").text().substring(7));
                List<PM25Data> pm25DataList = pm25DataDAO.findAll(Example.of(pm25Data));

                if(pm25DataList.isEmpty()){
                    pm25Data.setId(UUID.randomUUID().toString());
                   // pm25Data.setArea(allElements.select("body > div.container > div.span12.avg > div:nth-child(1) > div.city_name > h2").text());
                    pm25Data.setRemark(allElements.select("body > div.container > div.span12.avg > div:nth-child(1) > div.level > h4").text());
                    pm25Data.setAQI(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(1) > div.value").text());
                    pm25Data.setPM25In1h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(2) > div.value").text());
                    pm25Data.setPM10In1h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(3) > div.value").text());
                    pm25Data.setCOIn1h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(4) > div.value").text());
                    pm25Data.setNO2In1h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(5) > div.value").text());
                    pm25Data.setO3In1h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(6) > div.value").text());
                    pm25Data.setO3In8h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(7) > div.value").text());
                    pm25Data.setSO2In1h(allElements.select("body > div.container > div.span12.avg > div.span12.data > div:nth-child(8) > div.value").text());
                    pm25Data.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                    pm25DataList.add(pm25Data);
                    //pm25DataDAO.save(pm25Data);

                    Elements elements=allElements.select("#detail-data > tbody > tr:nth-child(n+1)");
                    for (Element e: elements) {
                        PM25Data  pm25DataPoint=new PM25Data();
                        pm25DataPoint.setCrawlingTime(LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
                        pm25DataPoint.setMonitorPoint(e.getElementsByTag("td").get(0).text());
                        pm25DataPoint.setAQI(e.getElementsByTag("td").get(1).text());
                        pm25DataPoint.setRemark(e.getElementsByTag("td").get(2).text());
                        pm25DataPoint.setPM25In1h(e.getElementsByTag("td").get(4).text());
                        pm25DataPoint.setPM10In1h(e.getElementsByTag("td").get(5).text());
                        pm25DataPoint.setCOIn1h(e.getElementsByTag("td").get(6).text());
                        pm25DataPoint.setNO2In1h(e.getElementsByTag("td").get(7).text());
                        pm25DataPoint.setO3In1h(e.getElementsByTag("td").get(8).text());
                        pm25DataPoint.setO3In8h(e.getElementsByTag("td").get(9).text());
                        pm25DataPoint.setSO2In1h(e.getElementsByTag("td").get(10).text());
                        pm25DataPoint.setCreateTime(allElements.select("body > div.container > div.span12.avg > div:nth-child(2) > div.live_data_time > p").text().substring(7));
                        pm25DataPoint.setArea(allElements.select("body > div.container > div.span12.avg > div:nth-child(1) > div.city_name > h2").text());
                        pm25DataPoint.setId(UUID.randomUUID().toString());
                        pm25DataList.add(pm25DataPoint);
                    }

                    pm25DataDAO.saveAll(pm25DataList);
                }

            }catch (Exception e){
                log.error(e.getMessage());
                continue;
            }

        }
    }
}
